using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;

public class MoveCameraByMouse : MonoBehaviour
{
    // 围绕旋转的目标物体
    private Transform target;
    public Transform targetOne;
    public Transform targetTwo;

    // 设置旋转角度
    public float x = 0f, y = 0f, z = 0f;
    public bool xFlag = false, yFlag = false;

    // 旋转速度值
    public float xSpeed = 140f, ySpeed = 100f, mSpeed = 5f;

    // y轴角度限制，设置成一样则该轴不旋转
    public float yMinLimit = -50, yMaxLimit = 80;

    // x轴角度限制，同上
    public float leftMax = -365, rightMax = 365;

    // 距离限制，同上
    public float distance = 3f, minDistance = 2f, maxDistance = 6f;

    // 阻尼设置
    public bool needDamping = true;

    public float damping = 3f;

    public float initX;

    public float initY;

    // 改变中心目标物体
    public void SetTarget(Transform transf)
    {
        target = transf;
    }

    void Start()
    {
        SetTarget(targetOne);
        Vector3 angles = transform.eulerAngles;

        x = angles.y;

        y = angles.x;
        //pers();
    }

    private Ray ray;
    private RaycastHit hit;
    GameObject obj;

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            // Debug.DrawLine(Input.mousePosition, Vector3.forward, Color.red);
            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log(hit.collider.gameObject.name);
                obj = hit.collider.gameObject;
                Debug.Log("点中： name = " + obj.name+ "点中： tag = " + obj.tag);
                
                //通过标签
                if (obj.tag == "Car")
                {
                    Debug.Log("点中" + obj.name);
                    //通过名字
                    if (obj.name.Equals("battery"))
                    {
                        SetTarget(targetTwo);
                    }
                    else
                    {
                        SetTarget(targetOne);
                    }
                }
            }
        }
    }

    void LateUpdate()
    {
        if (target)
        {
            if (Input.GetMouseButton(1))
            {
                // 判断是否需要反向旋转
                if ((y > 90f && y < 270f) || (y < -90 && y > -270f))
                {
                    x -= Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                }
                else
                {
                    x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                }

                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

                x = ClampAngle(x, leftMax, rightMax);

                y = ClampAngle(y, yMinLimit, yMaxLimit);
            }

            distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;

            distance = Mathf.Clamp(distance, minDistance, maxDistance);
            
            Quaternion rotation = Quaternion.Euler(y, x, z);

            Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);

            Vector3 position = rotation * disVector + target.position;
            
            // 阻尼感
            if (needDamping)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
                transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
            }
            else
            {
                transform.rotation = rotation;
                transform.position = position;
            }
        }
    }

    // 对数值进行限制；
    static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)

            angle += 360;

        if (angle > 360)

            angle -= 360;

        return Mathf.Clamp(angle, min, max);
    }

}